package com.reder.practice.service.impl;

import com.reder.practice.bean.User;
import com.reder.practice.bean.Video;
import com.reder.practice.common.dto.ServerResponse;
import com.reder.practice.common.enums.state.VideoState;
import com.reder.practice.common.util.FileOptUtil;
import com.reder.practice.common.util.PageUtil;
import com.reder.practice.common.util.SqlResultUtil;
import com.reder.practice.dao.VideoDao;
import com.reder.practice.service.VideoService;
import com.reder.practice.vo.VideoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 视频业务实现
 *
 * @author Red
 * <p>Email: 1318944013@qq.com</p>
 * <p>created by 2019/09/23 15:14</p>
 */
@Service
@Slf4j
public class VideoServiceImpl implements VideoService {

    @Value("${upload_file_path}")
    private String superDir;

    @Resource
    private VideoDao videoDao;

    @Override
    public ServerResponse addVideo(MultipartFile file, Video video, User user) {
        // 上传相对路径 例如 1/
        String tempPath = user.getId() + "/";
        // 尝试上传文件 拼接了目录：O://1/
        ServerResponse result = FileOptUtil.uploadFile(file, superDir + tempPath);
        if (!result.isSuccess()) {
            return result;
        }
        // video -> 数据库
        return SqlResultUtil.checkSqlResult(
                // 1/my.jpg
                videoDao.insert(buildVideo(video, tempPath + file.getOriginalFilename(), user)),
                VideoState.ADD_VIDEO_ERROR);
    }

    @Override
    public ServerResponse listVideo(int pageNumber, int sizeOfOnePage) {
        // 分页开始
        PageUtil.toPage(pageNumber, sizeOfOnePage);
        // 查询视频列表
        List<VideoVo> list = videoDao.queryVideo(pageNumber, sizeOfOnePage);
        return ServerResponse.response(VideoState.SUCCESS, PageUtil.pageInfo(list));
    }

    @Override
    public ServerResponse listVideoById(int pageNumber, int sizeOfOnePage, User user) {
        // 判断用户信息
        if (user == null || user.getId() == null) {
            return ServerResponse.response(VideoState.USER_INFO_IS_NULL);
        }
        // 分页开始
        PageUtil.toPage(pageNumber, sizeOfOnePage);
        // 查询个人视频列表
        List<VideoVo> list = videoDao.queryVideoById(pageNumber, sizeOfOnePage, user.getId());
        return ServerResponse.response(VideoState.SUCCESS, PageUtil.pageInfo(list));
    }

    @Override
    public ServerResponse updateVideo(Video video) {
        // 检查待更新的video
        ServerResponse response = checkUpdateVideo(video);
        if (!response.isSuccess()) {
            return response;
        }
        return SqlResultUtil.checkSqlResult(videoDao.updateByPrimaryKeySelective(video),
                VideoState.UPDATE_VIDEO_ERROR);
    }

    @Override
    public ServerResponse getVideo(long videoId) {
        Video video = videoDao.selectByPrimaryKey(videoId);
        if (video == null) {
            return ServerResponse.response(VideoState.VODEO_NOT_EXIST);
        }
        return ServerResponse.response(VideoState.SUCCESS, video);
    }

    @Override
    public ServerResponse deleteVideo(long videoId) {
        if (!videoIsExist(videoId)) {
            return ServerResponse.response(VideoState.VODEO_NOT_EXIST);
        }
        return SqlResultUtil.checkSqlResult(videoDao.deleteByPrimaryKey(videoId),
                VideoState.DELETE_VIDEO_ERROR);
    }

    // 构建video
    private Video buildVideo(Video video, String filePath, User user) {
        video.setFilePath(filePath);
        video.setCreateTime(new Date());
        video.setUserId(user.getId());
        video.setCommentTimes(0L);
        video.setPlayTimes(0L);
        video.setStarTimes(0L);
        return video;
    }

    // 检查待更新的video
    private ServerResponse checkUpdateVideo(Video video) {
        if (video == null || video.getId() == null) {
            return ServerResponse.response(VideoState.UPDATE_PARAM_ERROR);
        }
        if (!videoIsExist(video.getId())) {
            return ServerResponse.response(VideoState.VODEO_NOT_EXIST);
        }
        return ServerResponse.response(VideoState.SUCCESS);
    }

    // 视频是否存在
    private boolean videoIsExist(long videoId) {
        return videoDao.selectByPrimaryKey(videoId) != null;
    }

}
